package com.ruoyi.sso.util.smsUtil;

import java.io.IOException;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.alibaba.fastjson.JSONException;
import com.github.qcloudsms.SmsMultiSender;
import com.github.qcloudsms.SmsMultiSenderResult;
import com.github.qcloudsms.SmsSingleSender;
import com.github.qcloudsms.SmsSingleSenderResult;
import com.github.qcloudsms.httpclient.HTTPException;
import com.github.qcloudsms.httpclient.ProxyHTTPClient;
import com.ruoyi.common.config.Global;

/**
 * 阿里云sms
 * 
 * @author Administrator
 *
 */
@Component
public class Qcloudsms {

	

	@Autowired
	private Global ruoYiConfig;
	
	/**
	 * 单条发送
	 * 
	 * @param phoneNumbers
	 * @return
	 */
	public String SmsSingle(String[] phoneNumbers,Integer templateId) {

		if (!ruoYiConfig.isQcloudsmsEnable()) {
			return "短信服务未开启";
		}

		try {
			String[] params = { "5678" };
			SmsSingleSender ssender = new SmsSingleSender(Integer.parseInt(ruoYiConfig.getQcloudsmsAppid()),
					ruoYiConfig.getQcloudsmsAppkey());
			SmsSingleSenderResult result = ssender.sendWithParam("86", phoneNumbers[0], templateId, params,
					ruoYiConfig.getQcloudsmsSmsSign(), "", "");
			System.out.println(result);
			return result.toString();
		} catch (HTTPException e) {
			// HTTP 响应码错误
			e.printStackTrace();
		} catch (JSONException e) {
			// JSON 解析错误
			e.printStackTrace();
		} catch (IOException e) {
			// 网络 IO 错误
			e.printStackTrace();
		}
		return null;
	}

	/**
	 * 群发
	 * 
	 * @param phoneNumbers
	 * @return
	 */
	public String SmsMulti(String[] phoneNumbers,Integer templateId) {

		if (!ruoYiConfig.isQcloudsmsEnable()) {
			return "短信服务未开启";
		}
		try {
			String[] params = { "5678" };
			SmsMultiSender msender = new SmsMultiSender(Integer.parseInt(ruoYiConfig.getQcloudsmsAppid()),
					ruoYiConfig.getQcloudsmsAppkey());
			SmsMultiSenderResult result = msender.sendWithParam("86", phoneNumbers, templateId, params,
					ruoYiConfig.getQcloudsmsSmsSign(), "", "");
			System.out.println(result);
			return result.toString();
		} catch (HTTPException e) {
			// HTTP 响应码错误
			e.printStackTrace();
		} catch (JSONException e) {
			// JSON 解析错误
			e.printStackTrace();
		} catch (IOException e) {
			// 网络 IO 错误
			e.printStackTrace();
		}

		return null;
	}

	/**
	 * 代理发送
	 * 
	 * @param phoneNumbers
	 * @return
	 */
	public String ProxyHTTPClient(String[] phoneNumbers,Integer templateId) {
		if (!ruoYiConfig.isQcloudsmsEnable()) {
			return "短信服务未开启";
		}
		try {
			// 创建一个代理 httpclient
			ProxyHTTPClient httpclient = new ProxyHTTPClient("127.0.0.1", 8080, "http");

			String[] params = { "5678" };
			SmsSingleSender ssender = new SmsSingleSender(Integer.parseInt(ruoYiConfig.getQcloudsmsAppid()),
					ruoYiConfig.getQcloudsmsAppkey(), httpclient);
			SmsSingleSenderResult result = ssender.sendWithParam("86", phoneNumbers[0], templateId, params,
					ruoYiConfig.getQcloudsmsSmsSign(), "", ""); // 签名参数未提供或者为空时，会使用默认签名发送短信
			System.out.println(result);
			return result.toString();
		} catch (HTTPException e) {
			// HTTP 响应码错误
			e.printStackTrace();
		} catch (JSONException e) {
			// JSON 解析错误
			e.printStackTrace();
		} catch (IOException e) {
			// 网络 IO 错误
			e.printStackTrace();
		}
		return null;
	}

}
